# -*- coding: utf-8 -*-
import numpy as np
from PIL import ImageGrab
import cv2
from win32api import GetSystemMetrics
import time


def get_screen_area_as_image(area=(0, 0, GetSystemMetrics(0), GetSystemMetrics(1))):
    screen_width = GetSystemMetrics(0)
    screen_height = GetSystemMetrics(1)

    # h, w = image.shape[:-1]  # height and width of searched image

    x1 = min(int(area[0]), screen_width)
    y1 = min(int(area[1]), screen_height)
    x2 = min(int(area[2]), screen_width)
    y2 = min(int(area[3]), screen_height)

    search_area = (x1, y1, x2, y2)

    img_rgb = ImageGrab.grab().crop(search_area).convert("RGB")
    img_rgb = np.array(img_rgb)  # convert to cv2 readable format (and to BGR)
    img_rgb = img_rgb[:, :, ::-1].copy()  # convert back to RGB

    return img_rgb

get_screen_area_as_image()

